package com.project.XXXX.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.project.XXXX.controllerInterface.ICityChoosenController;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.model.tbl_division;
import com.project.XXXX.tools.ProjectTools;

@Controller

public class CityChoosenapiImpl implements ICityChoosenController {

	@Resource
	private tbl_divisionMapper tbl_divisionMapper;

	@RequestMapping("/api/cityCode/getProvince.do")
	public void getprovince(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		List<tbl_division> list = null;
		list = tbl_divisionMapper.selectProvinces();
		String json = new Gson().toJson(list);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/api/cityCode/getCities.do")
	public void getCities(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		List<tbl_division> list = null;
		String Province = request.getParameter("Province");
		Province = Province.substring(0, 2) + "%";
		list = tbl_divisionMapper.selectCitysByProvince(Province);
		String json = new Gson().toJson(list);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/api/cityCode/getCountries.do")
	public void getCountries(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		List<tbl_division> list = null;
		String City = request.getParameter("City");

		// 直辖市
		if (StringUtils.startsWithAny(City, "31", "50", "11", "12")) {
			City = City.substring(0, 2) + "%";
		} else {
			City = City.substring(0, 4) + "%";
		}
		list = tbl_divisionMapper.selectCountriesByCity(City);

		String json = new Gson().toJson(list);
		ProjectTools.ResponseOut(response, json);

	}

	

	@RequestMapping("/api/cityCode/getPlaceByName.do")
	public void getPlaceCodeByName(@RequestParam("PlaceName") String PlaceName, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		Map<String, String> map = new HashMap<>();
		tbl_division tb = tbl_divisionMapper.getByPlaceName(PlaceName);
		String json;
		if (tb != null) {
			json = new Gson().toJson(tb);
		} else {
			map.put("status", "failure");
			map.put("error_description", "can't found");
			json = new Gson().toJson(map);
		}

		ProjectTools.ResponseOut(response, json);
	}
}
